import 'package:flutter/material.dart';

import '../common/flatPanel.dart';
import '../entry.dart';
import '../utils/screw.dart';

class IndexPage extends StatefulWidget {
  IndexPage({Key key, this.title: Entry.Index}) : super(key: key);

  final String title;

  @override
  _IndexPageState createState() {
    return _IndexPageState();
  }
}

class _IndexPageState extends State<IndexPage> {
  final _items = <Widget>[];

  void _prepareDate() {
    Entry.props.forEach((key, value) {
      _items.add(RaisedButton(
        child: Text(key, style: TextStyle(fontSize: 24)),
        color: Screw.getRandomColor(),
        onPressed: () => Navigator.pushNamed(context, value),
      ));
    });
  }

  @override
  void initState() {
    super.initState();
    _prepareDate();
  }

  // MainAxisAlignment（主轴）就是与当前控件方向一致的轴，
  // 而CrossAxisAlignment（交叉轴）就是与当前控件方向垂直的轴

  // https://www.jianshu.com/p/fb3bf633ee12
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text(widget.title),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.settings),
            tooltip: 'DSEditor',
            onPressed: () => Navigator.pushNamed(context, Routes.DSEditor),
          )
        ],),
        body: FlatPanel(
          child: GridView.builder(
            padding: EdgeInsets.all(8),
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                //横轴元素个数
                crossAxisCount: 3,
                //纵向（主轴）间距
                mainAxisSpacing: 60.0,
                //横向（交叉轴）间距
                crossAxisSpacing: 40.0,
                //子组件宽高长度比例
                childAspectRatio: 2.0),
            itemCount: _items.length,
            itemBuilder: (context, index) {
              return _items[index];
            },
          ),
        ));
  }
}
